$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/sysactive/typelist',
        datatype: "json",
        colModel: [			
			{ label: '活动id', name: 'activeId', index: "activeId", width: 45, key: true },
			{ label: '活动名称', name: 'title', width: 75 },
			{ label: '活动类型', name: 'type', width: 60,formatter:function(value,options,row){
			        return value==1?'天天奖励':value==2?'周期奖励':'新手奖励';
                } },
            { label: '创建时间', name: 'createTime', width: 45},
            { label: '是否删除', name: 'isDel',  width: 75,formatter:function(value,options,row){
                return value==1?'<span class="label label-success">进行中</span>':'<span class="label label-danger">已删除</span>';
                }},
            { label: '操作',name:'activeInfoId',formatter:function(value, options, row){

                    return '<span class = "btn btn-success" onclick = "toUpdate('+value+')"> 修改 </span>&nbsp;&nbsp;'+
                        '<span class="btn btn-danger" onclick="delActive('+value+')">删除</span>&nbsp;&nbsp;';
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });



});


function delActive(id){
    confirm("确定要删除选中该活动？",function(){
        vm.active.isDel=2;
        vm.active.activeId=id;
        $.ajax({
            type: "POST",
            url: baseURL + "sys/sysactive/updatebyid",
            contentType: "application/json",
            data: JSON.stringify(vm.active),
            success: function(r){
                if(r.code == 0){
                    alert('操作成功', function(){
                        vm.reload();
                    });
                }else{
                    alert(r.msg);
                }
            }
        });

    });
}


function toUpdate(aid){
    $.ajax({
        type: "POST",
        url: baseURL + "sys/sysactive/querybyid",
        dataType: "json",
        data: {"activeId":aid},
        success: function(r){
            if(r.code == 0){
                vm.showList = false;
                vm.showAdd=true;
                vm.showUpdate=false;

                vm.active=r.active;
            }else{
                alert(r.msg)

            }

        }
    });
}

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            title:null,
            isDel:null
        },
        showList: true,
        showAdd:false,
        showCycle:false,
        showUpdate:false,
        title:null,
        active:{
            activeId:null,
            title:null,
            type:null,
            createTime:null,
            isDel:null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        clean:function(){
            vm.showList = true;
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'title': vm.q.title,'isDel':vm.q.isDel},
                page:1
            }).trigger("reloadGrid");
        },
        add: function(){
            vm.showList = false;
            vm.showAdd=true;
            vm.showUpdate=false;
            vm.title = "新增";
            vm.active={
                activeId:null,
                title:null,
                type:null,
                createTime:null,
                isDel:null
            }


        },
        insert:function(){


            vm.activeInfo.startDate=$("#instart").val();
            vm.activeInfo.endDate=$("#inend").val();
            $.ajax({
                url: baseURL + "sys/sysactiveinfo/add",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(vm.activeInfo),
                success: function (r) {
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            })
        },


        update: function () {
            vm.activeInfo.startDate=$("#instart").val();
            vm.activeInfo.endDate=$("#inend").val();
            $.ajax({
                url: baseURL + "sys/sysactiveinfo/update",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(vm.activeInfo),
                success: function (r) {
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            })
        },

        reload: function () {
            vm.showList = true;
            vm.showCycle=false;
            vm.showUpdate=false;
            vm.showAdd=false;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'title': vm.q.title,'isDel':vm.q.isDel},
                page:1
            }).trigger("reloadGrid");
        },
    }
});

